import pathToRegexp from 'path-to-regexp/index';
import {
    getOrgan,
    postOrgan,
    deleteOrgan,
    getTXLOUT,
    getTXL,
    postTXL,
    deleteTXL,
    postMaillist,
    deleteMaillist,
    putMaillist,
    getAllRoleOne,
    getAllRoleTow,
    postOrganRole,
    putMaillist_tow,
    postMaillist_tow,
    putUserLeave,
    getTXLWB,
} from 'services/app';
export default {
    namespace: 'organ',
    state: {
        bodyWidth:document.body.clientWidth,
        openKeys:{key:""},
        typeofZZ:'1',
        record:{},
    },
    subscriptions: {
        setupHistory ({ dispatch, history }) {
            history.listen((location) => {
                const match = pathToRegexp('/home').exec(location.pathname);
            });
        },
        setup ({ dispatch }) {},
    },
    effects: {
        //查询组织结构
        *getOrgan ({ payload }, { call, put }){
            console.log(`1111`, payload);
            const {data} = yield call(getOrgan, payload);
            yield put({ type: 'save', payload: {getOrganData:data} });
        },
        //查询组织结构对比
        *getOrganValue ({ payload }, { call, put }){
            const {data} = yield call(getOrgan, payload);
            let getOrganValueData={'000':true};
            data && data.map(item =>{
                getOrganValueData[item.id] = true;
            });
            yield put({ type: 'save', payload: {getOrganValueData:getOrganValueData} });
        },
        //添加组织
        *postOrgan ({ payload }, { call, put }){
            const data = yield call(postOrgan, payload);
            if(data.code == 0){
                const {data} = yield call(getOrgan, {type:payload.type});
                yield put({ type: 'save', payload: {getOrganData:data,visible:false,visible_4:false} });
            }
        },
        //删除组织
        *deleteOrgan ({ payload }, { call, put }){
            const data = yield call(deleteOrgan, {id:payload.id});
            if(data.code == 0){
                const {data} = yield call(getOrgan, {type:payload.type});
                yield put({ type: 'save', payload: {getOrganData:data} });
            }
        },
        //获取通讯录
        *getTXL ({ payload }, { call, put }){
            const {data} = yield call(getTXL, payload);
            yield put({ type: 'save', payload: {getTXLData:data}});
        },
        //获取外部通讯录
        *getTXLWB ({ payload }, { call, put }){
            const {data} = yield call(getTXLWB, payload);
            yield put({ type: 'save', payload: {getTXLData:data}});
        },
        //获取全部通讯录
        *getMaillist ({ payload }, { call, put }){
            const {data} = yield call(getTXLOUT, payload);
            yield put({ type: 'save', payload: {getMaillistData:data,visibleTXL:true}});
        },
        //添加通讯录
        *postTXL ({ payload }, { call, put }){
            const data = yield call(postTXL, payload);
            if(data.code == 0){
                yield put({ type: 'getTXL', payload: {
                    id:payload.organ_id,
                    type:payload.type,
                    searchValue:payload.searchValue || '',
                }});
                yield put({ type: 'save', payload: {visible_5:false}});
            }
        },
        //移除通讯录
        *deleteTXL ({ payload }, { call, put }){
            const data = yield call(deleteTXL, payload);
            if(data.code == 0){
                // yield put({ type: 'getTXL', payload: {
                //     id:payload.f_id,
                //     type:'2',
                //     searchValue:payload.searchValue || '',
                // }});
                // yield put({ type: 'save', payload: {visibleTow:false}});
            }
        },
        //添加组织成员
        *postMaillist ({ payload }, { call, put }){
            const data = yield call(postMaillist, payload);
            if(data.code == 0){
                yield put({ type: 'getTXL', payload: {
                    id:payload.f_id,
                    type:payload.type?payload.type:'1',
                    searchValue:payload.searchValue || '',
                }});
                yield put({ type: 'save', payload: {visibleTow:false}});
            }
        },
        //修改通讯录
        *putMaillist ({ payload }, { call, put }){
            const data = yield call(putMaillist, payload);
            if(data.code == 0){
                yield put({ type: 'getTXL', payload: {
                    id:payload.f_id,
                    type:payload.type?payload.type:'1',
                    searchValue:payload.searchValue || '',
                }});
                yield put({ type: 'save', payload: {visibleTow:false}});
            }
        },
        //添加组织成员
        *postMaillist_tow ({ payload }, { call, put }){
            const data = yield call(postMaillist_tow, payload);
            if(data.code == 0){
                yield put({ type: 'getTXLWB', payload: {
                    id:payload.f_id,
                    // type:'3',
                    // searchValue:payload.searchValue || '',
                }});
                yield put({ type: 'save', payload: {visibleTow:false}});
            }
        },
        //修改通讯录
        *putMaillist_tow ({ payload }, { call, put }){
            const data = yield call(putMaillist_tow, payload);
            if(data.code == 0){
                yield put({ type: 'getTXLWB', payload: {
                    id:payload.f_id,
                    // type:'3',
                    // searchValue:payload.searchValue || '',
                }});
                yield put({ type: 'save', payload: {visibleTow:false}});
            }
        },
        //删除通讯录
        *deleteMaillist ({ payload }, { call, put }){
            console.log(2222,payload);
            const data = yield call(deleteMaillist, payload);
            // if(data.code == 0){
            //     yield put({ type: 'getTXL', payload: {
            //         id:payload.f_id,
            //         type:payload.type?payload.type:'1',
            //         searchValue:payload.searchValue || '',
            //     }});
            // }
        },
        //获取通讯录角色
        *getAllRole ({ payload }, { call,put }){
            const {data} = yield call(getAllRoleOne, {o_id:'111111'});//部门
            const dataTow = yield call(getAllRoleTow, {o_id:'222222'});//角色
            yield put({ type: 'save', payload: {getBMZZData:dataTow.data,menuTreeOne_5:data} });
        },
        //添加角色
        *postOrganRole ({ payload }, { call,put }){
            const data = yield call(postOrganRole, payload);
            if(data.code == 0){
                const {data} = yield call(getOrgan, {type:'2'});
                yield put({ type: 'save', payload: {getOrganData:data,visible_4:false} });
            }
        },
        //输入等于输出
        *inputOut ({ payload }, { put }){
            yield put({ type: 'save', payload: payload });
        },

        *userLeave ({ payload}, {call, put}) {
            console.log(payload);
            const data = yield call(putUserLeave, payload);

        },
    },
    reducers: {
        save (state, { payload }) {
            return { ...state, ...payload };
        },
    },
};
